package com.neu.ui.dashboard;

import com.android.volley.Request;
import com.google.gson.Gson;
import com.neu.bean.NewsBean;
import com.neu.bean.NewsCategory;
import com.neu.util.VolleyUtil;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

public class DashboardViewModel extends ViewModel {

    private MutableLiveData<NewsCategory> mNewsCategory;
    private MutableLiveData<NewsBean> mNewsBean;
    private Gson gson;

    public DashboardViewModel() {
        mNewsCategory = new MutableLiveData<>();
        mNewsBean = new MutableLiveData<>();
       gson=new Gson();
    }

    public LiveData<NewsBean> getNews(){
        return mNewsBean;
    }

    public void loadNews(String type){
        String url="/prod-api/press/press/list?type="+type;
        VolleyUtil.request(Request.Method.GET, url, null, new VolleyUtil.BackJson() {
            @Override
            public void success(String json) {
                NewsBean newsBean=gson.fromJson(json,NewsBean.class);
                mNewsBean.setValue(newsBean);
            }
        });
    }

    public LiveData<NewsCategory> getNewsCategory() {
        String url="/prod-api/press/category/list";
        VolleyUtil.request(Request.Method.GET, url, null, new VolleyUtil.BackJson() {
            @Override
            public void success(String json) {
                NewsCategory newsCategory=gson.fromJson(json,NewsCategory.class);
                mNewsCategory.setValue(newsCategory);
            }
        });

        return mNewsCategory;
    }
}